package com.org.csu.converter;

/**
 * @author YigongLily
 * @version V1.0
 * Copyright (c) 2024, t8669@qq.com All Rights Reserved.
 * @ProjectName:e-commerce-back
 * @Title: KeywordRecordConverter
 * @Package com.org.csu.converter
 * @Description: kwrconverter
 * @date 2024/12/15 12:04
 */

import com.org.csu.dto.KeyRecordDTO;
import com.org.csu.entity.KeyRecord;
import com.org.csu.entity.Word;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class KeywordRecordConverter {
    public List<Word> convertToEntityAttribute(String dbData) {
        if (dbData == null || dbData.isEmpty()) {
            return null;
        }
        return Arrays.stream(dbData.split(";"))
                .map(pair -> pair.split(","))
                .filter(split -> split.length == 2)
                .map(split -> new Word(split[0], Integer.parseInt(split[1])))
                .collect(Collectors.toList());
    }

    public KeyRecordDTO convertToKeyRecord(KeyRecord keyRecord) {
        KeyRecordDTO keyRecordDTO = new KeyRecordDTO();
        keyRecordDTO.setRecordId(keyRecord.getRecordId());
        keyRecordDTO.setKeyword(keyRecord.getKeyword());
        keyRecordDTO.setMidword(convertToEntityAttribute(keyRecord.getMidword()));
        keyRecordDTO.setCompword(convertToEntityAttribute(keyRecord.getCompword()));
        keyRecordDTO.setDate(keyRecord.getDate());
        keyRecordDTO.setRate(keyRecord.getRate());
        keyRecordDTO.setTrend(convertToEntityAttribute(keyRecord.getTrend()));
        return keyRecordDTO;
    }
}